const { Client } = require("basic-ftp");
const {exec} = require("child_process")
// const fs = require('fs');
const path = require('path');

const targetFile = '/';
const uploadFiles = path.resolve(__dirname, './dist');

exec('yarn build', (error, stdout, stderr) => {
    if (error) {
        console.error('打包失败:', error.message);
        return;
    }
    console.log('打包成功，开始上传 dist 文件夹内容...');
    uploadFile();
})



const uploadFile  = async () => {
    const client = new Client()
    client.ftp.verbose = true
    try {
        await client.access({
            host: "121.41.102.102",
            user: "job_hesw_xin",
            password: "19841700.wan",
            secure: false,
            port: 21,
        })
        console.log("连接成功，开始上传文件...");
        client.trackProgress(info => {
            console.log(`上传文件名：${info.name}---> 类型：${info.type}---> 字节：${info.bytes}`)
        })
        await client.ensureDir(`${targetFile}`)
        await client.clearWorkingDir()
        await client.uploadFromDir(uploadFiles, `${targetFile}`)
     
    }
    catch(err) {
        console.log(err)
    }
    client.close()
}
